package player;

import gui.gameGui;
import gui.mainGui;

import java.io.IOException;
import java.net.*;
import java.util.*;
import p2pEngine.*;
import netPacks.message;

public class msgHandler extends Thread{
    private static volatile msgHandler instance = null;
    public LinkedList<message> msgQ = new LinkedList<message>();
	
	private msgHandler(){}
	
	public static msgHandler getInstance() {
        if (instance == null){
            synchronized (msgHandler.class){
                    if (instance == null){
                        instance = new msgHandler();
                    }
            }
        }
        return instance;
    }

   
   public synchronized message getNextMessage(){
	   return this.msgQ.removeFirst();
   }
   
   
   public synchronized void run(){
 		while(true){
			boolean temp = this.msgQ.isEmpty();
			try {
				Thread.sleep(100);
			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			}
 			if(!temp){
 				message currMessage = getNextMessage();
 				String action = currMessage.getAction();
 					switch(action){
 					case "waitPlease":
 						System.out.println("Please wait, finding opponent...");
 						break;
 					case "match":
 						System.out.println("match message received.");
 						player.getInstance().sendMatched();
 						player.getInstance().setMatched(true);
 						player.getInstance().setOpIp(currMessage.getUserId());
 						mainGui.getInstance().setFrameVisibility(false);
 						//mainGui.getInstance().terminateMainGui();
 						player.getInstance().startP2P();
 						gameGui.getInstance().setGuiMode();
 						player.getInstance().GAME = gameGui.getInstance();
 						gameGui.getInstance().display();
 						break;
 					case "youAreHost":
 						player.getInstance().setTurn(true);
 						player.getInstance().setIsHost(true);
 						break;
 					case "refreshCon":
 						System.out.println("Cannot connect multiple clients. Connection deleted. Try again.");
 						break;
 					case "backupAddress":
 						try {
 							String tempStr = currMessage.getData();
 							String[] str = tempStr.split("/");
 							tempStr = str[1];
 							InetAddress tempAdd = InetAddress.getByName(tempStr);
							clientToServer.getInstance().setBackupHost(tempAdd);
						} 
 						catch (UnknownHostException e) {
							e.printStackTrace();
						}
							System.out.println("Backup host is: "+clientToServer.getInstance().getBackupHost());
 						break;
 					case "aWord":
 						player.getInstance().setSentence(currMessage.getData());
 						String tmp = player.getInstance().scrm.scrambleWord(currMessage.getData());
 						player.getInstance().setScrambled(tmp);
 						gameGui.getInstance().setIncomingWordText(tmp);
 						gameGui.getInstance().setGuessWordEditable(true);
 						gameGui.getInstance().setGuessButtonEnable(true);
 						gameGui.getInstance().startTimer();
 						break;
 					case "playAgain":
 						player.getInstance().incrementPlayAgain();
 						break;
 					case "playerName":
 						player.getInstance().setOpName(currMessage.getData());
 						break;
 					case "forfiet":
 						
 						break;
 					case "ping":
 						System.out.println("server connection status OK.");
 						pingChecker.getInstance().pingCnt = 3;
 						break;
 					case "opName":
 						player.getInstance().setOpName(currMessage.getData());
 						gameGui.getInstance().setOpName(currMessage.getData());
 						break;
 					default:
 						System.err.println("unknown message received:\n Message Action specified: "+currMessage.getAction());
 						break;
 					}	
 			}
 		}
 	}
   

}
